package Tools

import (
	"fmt"
	"os"
	"strings"
	"time"
)

//保存评判结果
type JundgeType struct {
	JundgeResType string
	JundgeSecode  int
	JundgeMenory  int
	Meg           string
}

const (
	Ac           = "Accepted_AC"
	CompileError = "Compile_Error_CE"
	MeoneryError = "Wrong_Answer_WA"
	RunningError = "RunningError_RE"
	ResultError  = "ResultError_ER"
)

//用户代码，输入测试数据，正确结果,语言类型
func StartCompile(Usercode string, input []string, result []string, LugType string) JundgeType {
	switch LugType {
	case "Java":
		return javaCompile(Usercode, input, result)
	case "Python3":
		return Python3Compile(Usercode, input, result)
	case "C":
		return GccCompile(Usercode, input, result)
	case "go":
		return GoCompile(Usercode, input, result)
	}
	return JundgeType{}
}

// var localPath = "/home/yh/OjProject/JudgeServer/"

func GccCompile(Usercode string, input []string, result []string) JundgeType {
	FIleName := WriteCodFile(Usercode, ".c")
	args := make([]string, 4)
	args[0] = "-o test.out "
	args[1] = FIleName
	return Compiles("/usr/bin/bash", args, input, result)

}

//写入一个文件 作为编译文件 返回文件名
func WriteCodFile(code string, types string) string {
	name := "tests"
	f, err := os.Create("./CodingFIle/" + name + types)
	if err == nil {
		f.Write([]byte(code))
	}
	return "./CodingFIle/" + name + types
}
func GoCompile(Usercode string, input []string, result []string) JundgeType {
	FileTempter := WriteCodFile(Usercode, ".go")
	args := make([]string, 3)
	args[0] = "run"
	args[1] = FileTempter
	return Compiles("/home/yh/golang/go/bin/go", args, input, result)
}
func CsharpCompile(Usercode string, input []string, result []string) JundgeType {
	return JundgeType{}
}
func RemoveTrim(code string) string {
	code = strings.Replace(code, " ", "", -1)
	code = strings.Replace(code, "\n", "", -1)
	return code

}

func Compiles(CompilePath string, args []string, input []string, result []string) JundgeType {
	JundRes := JundgeType{}
	JundRes.JundgeResType = AC
	start := time.Now().Unix()
	for i := 0; i < len(result); i++ {
		if i < len(input) {
			args[2] = input[i]
		}
		res := StartProess(CompilePath, args)
		if res.CompileType == AC {
			result[i] = RemoveTrim(result[i])
			res.Meg = RemoveTrim(res.Meg)
			fmt.Println("res-" + res.Meg + "|result" + result[i])
			if !strings.Contains(res.Meg, result[i]) {
				JundRes.JundgeResType = ResultError
				break
			}
		} else {
			JundRes.JundgeResType = CompileError
			JundRes.Meg = res.Meg
			break
		}
	}
	JundRes.JundgeMenory = 12
	JundRes.JundgeSecode = int(time.Now().Unix()) - int(start)
	return JundRes
}
func Python3Compile(Usercode string, input []string, result []string) JundgeType {
	FileTempter := WriteCodFile(Usercode, ".py")
	args := make([]string, 3)
	args[0] = FileTempter
	args[1] = "-o"
	return Compiles("/usr/bin/python3", args, input, result)
}
func javaCompile(Usercode string, input []string, result []string) JundgeType {
	FileTempter := WriteCodFile(Usercode, ".java")
	args := make([]string, 3)
	args[0] = FileTempter
	args[1] = "-o"
	return Compiles("/usr/bin/java", args, input, result)
}
